<?php

class Admin extends Model_Abstract
{
	public function getAdminById($id)
	{
		try
		{
			$sql = "select * from admins where id=? limit 1";
			$result = $this->_connection->query($sql, array($id));
			if (count($result) == 0)
			{
				return false;
			}
			else
			{
				return $result[0];
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function valid($email, $password)
	{
		try
		{
			$sql = "select * from admins where email=? and password=? limit 1";
			$result = $this->_connection->query($sql, array($email, Util::encodePassword($password)));
			if (count($result) == 0)
			{
				return false;
			}
			else
			{
				return $result[0];
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function update($id, $email, $name, $company, $phone)
	{
		try
		{
			$sql = "update admins set email=?, name=?, company=?, phone=? where id=?";
			$this->_connection->execute($sql, array($email, $name, $company, $phone, $id));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function resetPassword($id, $password)
	{
		try
		{
			$sql = "update admins set password=? where id=?";
			$this->_connection->execute($sql, array(Util::encodePassword($password), $id));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getAllAdminsExPublishers()
	{
		try
		{
			$sql = "select * from admins where role != 'PUBLISHER'";
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getAllAdminsPublishers()
	{
		try
		{
			$sql = "select * from admins where role = 'PUBLISHER'";
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function add($email, $name, $company, $phone, $password, $role, $pub_id, $sub_id)
	{
		try
		{
			$sql = "insert into admins set email=?, name=?, company=?, phone=?, password=?, role=?, pub_id=?, sub_id=?";
			$result = $this->_connection->execute($sql, array($email, $name, $company, $phone, Util::encodePassword($password), $role, $pub_id, $sub_id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function deleteAdmin($id)
	{
		try
		{
			$sql = "delete from admins where id=?";
			$this->_connection->execute($sql, array($id));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function checkIfExists($email){
		try
		{
			$sql = "select * from admins where email=?";
			$result = $this->_connection->query($sql, array($email));
			if($result)
				return true;
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}
?>